/* 分公司二审修改页 */
<template>
  <div>
    <Card :bordered="false">
      <p slot="title">二审修改页</p>
      <table style="margin-bottom:20px" cellspacing="0" cellpadding="0" border="1" width="100%" ref="print">
        <tr>
          <th colspan="6">
            二审修改
          </th>
        </tr>
        <tr>
          <td>
            修改人员
          </td>
          <td width="16.666%" class="sizeStyle">
            {{info.RealName}}
          </td>
          <td width="16.666%">
            二审时间
          </td>
          <td width="16.666%" class="sizeStyle">
            <p v-for='(item,index) in info.AuditTime' :key='index'>{{item}}</p>
          </td>
          <td width="16.666%">
            修改完成时间
          </td>
          <td width="16.666%" class="sizeStyle">
            <p v-for='(item,index) in info.ChangeTime' :key='index'>{{item}}</p>
          </td>
        </tr>
      </table>
      <table style="margin-bottom:20px" cellspacing="0" cellpadding="0" border="1" width="100%">
        <tr>
          <th>
            二审修改清单
          </th>
        </tr>
        <tr>
          <td>
            <div style="width:99%;margin:auto;height:100%;">
              <Table style="border: none" border ref="selection" :columns="ershen" :data="info.data"></Table>
              <Row>
                <Col span="24" style="text-align: center;padding:10px">
                <Button type="primary" @click="passCS">通过二审</Button>
                </Col>
              </Row>
            </div>
          </td>
        </tr>
      </table>
      <table style="margin-bottom:20px" cellspacing="0" cellpadding="0" border="1" width="100%">
        <tr>
          <th colspan="4">
            二审修改清单2
          </th>
        </tr>
        <tr>
          <td width="25%" class="ivu-table-cell">二审人员</td>
          <td width="25%" class="sizeStyle">{{info.Audit}}</td>
          <td width="25%">二审本次意见时间</td>
          <td width="25%" class="sizeStyle">{{this.$moment().format('YYYY-MM-DD')}}</td>
        </tr>
        <tr>
          <td colspan="4">
            <div style="width:99%;margin:auto;height:100%;">
              <Table id="ershenXg" style="border: none" border ref="selection" :columns="ershenColumns" :data="ErshenXg" @on-row-click="getIndex"></Table>
              <Row>
                <Col span="24" style="text-align: center;padding:10px">
                <Button type="primary" @click="inputAdd('ErshenXg')">添加</Button>
                <Button type="error" @click="inputRemove('ErshenXg')">删除</Button>
                </Col>
              </Row>
            </div>
          </td>
        </tr>
        <tr>
          <td>
            报告（正文、附图、附件、附表）
          </td>

          <td colspan="3">
            <Button :disabled='!ProjectEnvOverview?true:false'  :to="ProjectEnvOverview" target='_blank' style="float:left;margin-left:50px">下载</Button>
            <div>
              <Upload with-credentials :before-upload="upLoadFun" :data='imgUpload' :action="shishi" ref="uploadButton">
                <Button>上传</Button>
              </Upload>
            </div>
            <!-- <Upload action="//jsonplaceholder.typicode.com/posts/">
              <Button>上传</Button>
            </Upload> -->
          </td>

        </tr>
      </table>

      <Row style="padding:40px 0">
        <Col span="12" style="text-align: left">
        <!-- <Button type="primary" @click="$print($refs.print)">打印</Button> -->
        <PrintButton Url="print/printAuditForm" :Project_id="info.Id || $route.query.ProjectId" Atype="分公司二审"></PrintButton>
        </Col>
        <Col span="12" style="text-align: right;">
        <Button type="primary" @click="submit('')">保存</Button>
        <Button type="primary" @click="submit(2)">发送意见</Button>
        </Col>
      </Row>
      <StepBar :status="4" />
    </Card>
  </div>
</template>

<script>
import libs from "../../../libs/uploads";
export default {
  data() {
    return {
      shishi:'',
      imgUpload: {
        Id: this.$route.query.ProjectId,
        Key: 'ImgUrl'
      },
      info: {},
      query: [],
      ershenLoading: true,
      ErshenXg: [],
      ershen: [
        {
          align: "center",
          type: "selection",
          width: 60,
          align: "center"
        },
        {
          align: "center",
          width: 80,
          title: "序号",
          type: "index"
        },
        {
          align: "center",
          title: "二审意见",
          key: "AuditOpinion"
        },
        {
          align: "center",
          title: "修改说明",
          key: "ReviseExplain"
        }
      ],
      ershenColumns: [
    
        {
          align: "center",
          title: "序号",
          type: "index",
          width: 80
        },
        {
          align: "center",
          title: "二审意见",
          key: "AuditOpinion",
          render: (h, { row, index }) => {
            return h("Input", {
              props: {
                type: "textarea",
                autosize: true,
                value: row.AuditOpinion
              },
              on: {
                "on-blur": val => {
                  this.ErshenXg[index].AuditOpinion = val.target.value;
                  console.log(
                    "失去焦点查看：" + this.ErshenXg[index].AuditOpinion
                  );
                }
              }
            });
          }
        },
        // {
        //   align: "center",
        //   title: "修改说明",
        //   key: "Revise_explain"
        // }
      ],
      ProjectEnvOverview: '',
      proType: 0,
      outTime: 0,
      csPass: 0,
    };
  },
  mounted() {
    let query = this.$route.query;
    this.query = query;
    this.findByProject(query.ProjectId, query.Atype);
    let delinfo = '';
    this.shishi = libs.uploads();
  },
  methods: {
    inputAdd(type) {
      this.ErshenXg.push({AuditTime:new Date().getTime()});
    },
    inputRemove(type) {
      // console.log(type)
      // console.log(this[type])
      // console.log(this.allIndex)
      // 保存回显，点删除要删除数据库里面，新删除不需要
      var delId = this[type][this.allIndex].Id
      if(this.delinfo == '审核中'){
        if(delId) {
          axios.post('/auditreply/deleteByid',{Id:delId}).then(res=>{
            if(res._STATUS==1) {
              this.$Message.success('删除成功！')
              this[type].splice(this.allIndex, 1); //删除数据库后本地也删除
            }else {
              this.$Message.error(res._MESSAGE)
            }
          })
        }else {
          this[type].splice(this.allIndex, 1);
          console.log(this[type] + ' 被删除了')
        } 
      } else if (this.delinfo == '未提交'){
        alert('审核未提交，无法进行修改')
        return
      } else if (this.delinfo == '已审核'){
        alert('审核已结束，无法进行修改')
        return
      }
    },
      getIndex(data, index, event) {
        this.allIndex = index;
      },
    findByProject(ProjectId, Atype) {
      // axios.get("/audit/findByProject2", {
      this.$post("audit.findByProject2", {
            Atype,
            ProjectId
        }).then(res => {
          // 把点保存时 填写数据过滤出来,初审修改清单中不会显示
          if(res.data&&res.data.length>0) {
            res.data = res.data.filter((ele)=>{
              if(ele.ChangeTime==null&&ele.ReviseExplain==null) {
                this.ErshenXg.push(ele)
              }
              return ele.ChangeTime!=null&&ele.ReviseExplain!=null
            })
          }
          this.getDownInfo()
          this.info = res;
          this.delinfo = res.Stats;
          var number = this.info.AuditTime.length + 1
          this.imgUpload.Name = '二审' + number + '批'
          this.ershenLoading = false;
          if(res.Stats == '审核中'){
            this.proType = 1
          }
        });
    },
    getDownInfo(){
        axios.post("wechatpublic/findImg",{
          Id:this.$route.query.ProjectId
        }).then((res)=>{
          if(this.info.Stats == '未提交'){
            this.ProjectEnvOverview =''
          }else{
            for(var i=0;i<res.data.length;i++){
              if(res.data[i].Status == '二审'){
                this.ProjectEnvOverview = res.data[i].ImgUrl
                console.log(this.ProjectEnvOverview)
              }
            }
          }
        })
      },
    upLoadFun(){
      this.$refs.uploadButton.clearFiles()
      if(this.proType != 1){
        this.$Message.warning('审核未开始，无法上传文件')
        return false
      }
    },
    submit(type) {
      if(this.outTime == 0){
        this.outTime = 1
        var bl = false;
        setTimeout(()=>{this.outTime = 0},5000)
        let query = this.query;
        let AuditReplyLists = this.ErshenXg;
        AuditReplyLists.map(i=>{
            if(i.AuditOpinion && i.AuditOpinion != ''){
              
            }else{
              bl = true
            }
          })
          if(bl){
            this.outTime = 0
            alert('请填写完整意见后，再发送意见！')
            return
          }
        var dataArr = Object.keys(AuditReplyLists).length <=0 ? [] : '123'
        for (let val of AuditReplyLists) {
          val["AuditId"] = query.Id;
          // val["Audit_time"] = this.$moment().format("YYYY-MM-DD HH:mm:ss");
        }
        axios.get("/audit/isAuditb", {
            params: {
              ProjectId: query.ProjectId,
              Btype: query.Atype,
              OK: 1,
              Save: type,
              AuditReplyLists: dataArr,
            }
        }).then(res => {
            let title = res._MESSAGE;
            if (res._STATUS == 1) {
              // if (Object.keys(AuditReplyLists).length <= 0) {
              //   this.$Modal.error({
              //     title: "请输入意见"
              //   });
              // }
              this.$Modal.success({
                title: title
              });
              if(type == 2){
              social({
                method: 'post',
                url: '/info/notice',
                params: {
                  content: this.info.ProjectName +'项目二审工程师提出审核意见，请尽快进行修改！',
                  app_name: '环评管控系统',
                  app: 'eao',
                  type: this.info.StaffNamesId + '',
                  uid: -1
                },
                }).then((res)=>{
                  console.log(res)
                }).catch((err)=>{
                  console.log(err)
                })
              }
              axios.post("/auditreply/saveOrUpdateM", {
                  AuditReplyLists:JSON.stringify(AuditReplyLists),
                  ProjectId: this.$route.query.ProjectId,
                  Atype: this.$route.query.Atype,
                  Aoc: '2',
                  Save: type
                })
                .then(res => {
                  // 保存、发送意见成功后跳转到正在审核页面
                  this.$router.push({name: 'myProjectCheck'})
                })
                .catch(err => {
                  this.$Modal.error({
                    title: "消息发送失败"
                  });
                });
            } else {
              this.$Modal.error({
                title
              });
            }
        });
      }else{
        console.log('别点了老铁')
      }
    },
    passCS() {
      if(this.csPass == 0){
        this.csPass = 1
        setTimeout(()=>{this.csPass = 0},5000)
        axios.get("/audit/isAuditb", {
            params: {
              ProjectId: this.$route.query.ProjectId,
              Btype: this.$route.query.Atype,
              OK: 0,
              AuditReplyLists: '123',
            }
        }).then(res => {
          let title = res._MESSAGE;
          if (res._STATUS == 1) {
            social({
              method: 'post',
              url: '/info/notice',
              params: {
                content: this.info.ProjectName +'项目已通过二审工程师审核，已经递交到初审！',
                app_name: '环评管控系统',
                app: 'eao',
                type: this.info.StaffNamesId + '',
                uid: -1
              },
              }).then((res)=>{
                console.log(res)
              }).catch((err)=>{
                console.log(err)
            })
            social({
              method: 'post',
              url: '/info/notice',
              params: {
                content: this.info.ProjectName +'项目已通过你的审核，已经递交到初审！',
                app_name: '环评管控系统',
                app: 'eao',
                type: this.info.auditor + '',
                uid: -1
              },
              }).then((res)=>{
                console.log(res)
              }).catch((err)=>{
                console.log(err)
            })
            this.$Modal.success({
              // title: title + ":通过初审进入正在进行操作"
              title: title,
              onOk:()=>{
                this.$router.push({name: 'myProjectCheck',params: {success:1}})
              }
            });
          } else {
            this.$Modal.error({
              title
            });
          }
        });
      }else{
      }
    }
  }
};
</script>

<style lang='less' scoped>
#ershenXg {
  .ivu-input {
    border: none !important;
    height: 30px;

    input {
      outline: medium;
    }
  }
}
/deep/ textarea.ivu-input {
  font-size: 12px;
}
.sizeStyle {
  font-size:12px;
}
</style>